<?php

/**
 * @author ThaiNM
 */
class ParamCheck {

    private $chkStatus = false;
    private $errMsg;
    private $errLogMsg;
    private $response = array();

    public function setResult($msg, $logMsg) {
        $this->chkStatus = true;
        $this->errMsg = $msg;
        $this->errLogMsg = $logMsg;
    }

    private function setRespone($msg) {
        $this->response['status'] = API_STATUS_400;
        $this->response['code'] = VALIDATE_PARAMS;
        $this->response['message'] = $msg;
    }

    public function getRespone() {
        return $this->response;
    }

    public function getStatus() {
        return $this->chkStatus;
    }

    public function getErrMsg() {
        return $this->errMsg;
    }

    public function getErrLogMsg() {
        return $this->errLogMsg;
    }

    public function chkEmail($value) {
        if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
            $this->setRespone(API_MSG_001);
            return false;
        }
        return true;
    }

    public function chkDouble($value, $key) {
        if (!filter_var($value, FILTER_VALIDATE_FLOAT)) {
            $this->setRespone(sprintf(API_MSG_016, $key));
            return false;
        }
        return true;
    }

    public function chkInt($value, $key) {
        if (!filter_var($value, FILTER_VALIDATE_INT)) {
            $this->setRespone(sprintf(API_MSG_018, $key));
            return false;
        }
        return true;
    }

    public function chkNotEmpty($value,$key) {
        if (empty($value)) {
            $this->setRespone(sprintf(API_MSG_002, $key));
            return false;
        }
        return true;
    }

    public function chkRequiredParams($required_fields) {
        $error = false;
        $error_fields = "";
        $request_params = array();
        $request_params = $_REQUEST;
        // Handling PUT request params
        if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
            $app = \Slim\Slim::getInstance();
            parse_str($app->request()->getBody(), $request_params);
        }
        foreach ($required_fields as $field) {
            if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
                $error = true;
                $error_fields .= $field . ', ';
            }
        }

        if ($error) {
            $this->setRespone(sprintf(API_MSG_002, substr($error_fields, 0, -2)));
            return false;
        }
        return true;
    }

}
